// pages/kind/kind.js
var wxNum;
Page({

  /**
   * 页面的初始数据
   */
  data: {
      kinds:[],
      font:"none",
      loading:"display"
  },
  CheckMaster: function () {
    var that = this;
    wx.request({
      url: "https://www.liuxuanlong.xyz/fenda/wx/checkMaster",
      data: {
        wxNum: wxNum
      },
      success: function (res) {

        if (res.data == 'no') {
          console.log('专家未登录')
          wx.redirectTo({
            url: '../login/login',
          })


        }
        else {
          console.log('专家一登陆')
          that.loadKind();

        }
      },
    })
  },

  tologin: function () {
    var that = this;
    wx.login({
      success: function (res) {
        if (res.code) {
          //发起网络请求
          wx.request({
            url: 'https://www.liuxuanlong.xyz/fenda/wx/wxLoginMaster',
            data: {
              code: res.code
            },
            success: function (res) {

              wx.setStorage({
                key: "login_uuId",
                data: res.data
              });
              wxNum = res.data;
              that.CheckMaster();
            }
          })
        } else {
          console.log('获取用户登录态失败！' + res.avatarUrl)
        }
      }
    })
  },
  Login: function () {
    var that = this;

    wx.checkSession({
      success: function () {
        console.log("enter");
        wx.getStorage({
          key: 'login_uuId',
          success: function (res) {

            wxNum = res.data;
            console.log(wxNum, '登录得到的openid');
            wx.request({
              url: "https://www.liuxuanlong.xyz/fenda/wx/checkLoginTime",
              data: {
                login_uuId: wxNum
              },
              success: function (res) {
                if (res.data == 'error') {
                  that.tologin();

                }
                else {

                  that.CheckMaster();
                }
              },

            })

          },
          fail:function(res){
            that.tologin();
          }
        })

      },
      fail: function () {
console.log("no cache");
        that.tologin();
      },

    })


    //

  },
  loadKind: function () {
    var that = this;
    wx.getStorage({
      key: 'login_uuId',
      success: function (res) {
        
        wxNum=res.data
        
        wx.request({
          url: "https://www.liuxuanlong.xyz/fenda/wx/findHospitalFieldAndIconByMasterOpenId",
          data: {
            login_uuid: wxNum
          },
          success: function (res) {
            for (var i = 0; i < res.data.length; i++)

              that.setData(
                {
                  kinds: res.data,
                  loading: "none",
                  font: "display"
                }
              );
            console.log(res.data, '医院的分类加载');
          },
        })
      },
      fail: function () {
        
      },
    })
   
  },
  toQuestion:function(e){
    var index = e.currentTarget.id;
    var kind = this.data.kinds[index].info;
    wxNum= wx.getStorageSync("login_uuId");
    wx.request({
      url: 'https://www.liuxuanlong.xyz/fenda/wx/checkMasterIsBlack',
      data:{
        wxNum:wxNum
      }, 
      success:function(res){
        if(res.data=='no'){
          wx.navigateTo({
            url: '../page/page?kind=' + kind,
          })
        }
        else{
          wx.showModal({
            title: '',
            content: '检测您发表了不正当言论，已暂停服务。请联系平台人员进行相关处理。',
          })
        }
      },
    })
    
   
    
   
   
  },
  
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
     this.Login();
    
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})